package dai.android.util

import android.app.Application
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import java.lang.reflect.Method

// android 工具箱
object AndroidUtils {

    // 通知是否开启
    // ctx 可以为 applicationContext
    fun isNotificationEnabled(ctx:Context):Boolean = NotificationManagerCompat.from(ctx).areNotificationsEnabled()

    // 反射去拿进程名
    fun getCurrentProcessNameByAT(): String? {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return Application.getProcessName()
        }

        var processName: String? = null
        try {
            val declaredMethod: Method = Class.forName("android.app.ActivityThread", false, Application::class.java.classLoader)
                .getDeclaredMethod("currentProcessName")
            declaredMethod.isAccessible = true
            val invoke = declaredMethod.invoke(null)
            if (invoke is String) {
                processName = invoke
            }
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return processName
    }
}